﻿using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace AutoKillThing
{
    static class KillList
    {
        private static string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\KillList.xml";
        public static Hashtable ActiveKills = new Hashtable();

        public static int Add(string theProcessName)
        {
            /** Make sure the file exists **/
            if (File.Exists(filePath))
            {
                if (ActiveKills.ContainsKey(theProcessName))
                {
                    return 1;
                }

                /** working with file in memory **/
                XmlReader reader = XmlReader.Create(filePath);
                XDocument xml = XDocument.Load(reader);
                reader.Close();

                /** create xml tree **/
                XElement wrk = xml.Descendants("kills").Last();
                wrk.Add(
                    new XElement("kill", 
                    new XAttribute("status", "True"),theProcessName
                    ));

                /** write it out **/
                StreamWriter sw = new StreamWriter(filePath);
                sw.Write("\n" + wrk + "\n");
                sw.Close();


                ActiveKills.Add(theProcessName, true);
                return 0;
            }
            return 1;
        }
        
        public static void Load()
        {
            /** we might not even have a file **/
            if (!File.Exists(filePath))
            {
                /** so we better make one **/
                //File.Create(filePath);
                CreateNewKillFile();
                return;
            }
            XElement xml = XElement.Load(filePath);
            string retVal = string.Empty;
            var query = from p in xml.Elements("kill")
                        select p;

            foreach (var record in query)
            {
                ActiveKills.Add(record.Value.ToString(), Boolean.Parse(record.Attribute("status").Value));
            }
        }

        public static void Remove(string theProcessName)
        {
           /** Make sure the file exists **/
            if (File.Exists(filePath))
            {
                /** working with file in memory **/
                var xml = XElement.Load(filePath);

                var query = from p in xml.Elements("kill")
                            where p.Value.ToString() == theProcessName
                            select p;

                foreach (var record in query)
                {
                    record.Remove();

                    /** write it out **/
                    var sw = new StreamWriter(filePath);
                    sw.Write(xml);
                    sw.Close();
                }

                ActiveKills.Remove(theProcessName);
            }
        }

        public static void ChangeStatus(string theProcessName, bool theNewStatus)
        {
            var xml = XElement.Load(filePath);

            var query = from p in xml.Elements("kill")
                        where p.Value.ToString() == theProcessName
                        select p;
            foreach (XElement item in query)
            {
                item.Attribute("status").Value = theNewStatus.ToString();
            }
            ActiveKills[theProcessName] = theProcessName;
            var sw = new StreamWriter(filePath);
            sw.Write(xml);
            sw.Close();
        }

        /// <summary>
        /// a cheesy way to create a new file until istall program
        /// </summary>
        public static void CreateNewKillFile()
        {
            var sw = new StreamWriter(filePath,false);
            sw.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8""?>");
            sw.WriteLine("<kills>");
            //sw.WriteLine(@"<kill status=""""></kill>");
            sw.WriteLine("</kills>");
            sw.Flush();
            sw.Close();

        }
    
    }
}
